package com.qbadroid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseAdapter {

	public static final String DB_NAME = "database.db";
	public static final String DB_TABLE = "myTable";
	private static final int DB_VERSION = 1;
	
	public static final String KEY_ID = "id";
	public static final String ID_OPTIONS = "INTEGER PRIMARY KEY AUTOINCREMENT";
	public static final String KEY_COUNTRY = "country";
	public static final String COUNTRY_OPTIONS = "TEXT NOT NULL";
	public static final int COUNTRY_COLUMN = 1;
	public static final String KEY_SHORTCUT = "shortcut";
	public static final String SHORTCUT_OPTIONS = "TEXT NOT NULL";
	public static final int SHORTCUT_COLUMN = 2;
	
	//table creation statement
	private static final String DB_CREATE = "create table " + DB_TABLE + " (" + KEY_ID + " " + ID_OPTIONS + ", " + KEY_COUNTRY + " " + COUNTRY_OPTIONS + ", " + KEY_SHORTCUT + " " + SHORTCUT_OPTIONS + ");";
	
	private SQLiteDatabase db;
	private Context context;
	private DatabaseHelper dbHelper;
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DB_CREATE);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
			onCreate(db);
		}
	}
	
	public DatabaseAdapter(Context context) {
		this.context = context;
		dbHelper = new DatabaseHelper(context, DB_NAME, null, DB_VERSION);
	}
	
	public DatabaseAdapter open()
	{
		db = dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close()
	{
		db.close();
	}
	
	public long insert(Country country)
	{
		ContentValues newCountry = new ContentValues();
		
		newCountry.put(KEY_COUNTRY, country.getCountryName());
		newCountry.put(KEY_SHORTCUT, country.getCountryShortcut());
		
		return db.insert(DB_TABLE, null, newCountry);
	}
	
	public boolean update(long index, Country country)
	{
		String where = KEY_ID + "=" + index;
		
		ContentValues updateCountry = new ContentValues();
		
		updateCountry.put(KEY_COUNTRY, country.getCountryName());
		updateCountry.put(KEY_SHORTCUT, country.getCountryShortcut());
		
		return db.update(DB_TABLE, updateCountry, where, null) > 0;
	}
	
	public boolean delete(long index)
	{
		String where = KEY_ID + "=" + index;
		return db.delete(DB_TABLE, where, null) > 0;
	}
	
	public void deleteAll()
	{
		db.delete(DB_TABLE, "1", null);
	}
	
	public boolean isEmpty()
	{
		String[] columns = {KEY_ID, KEY_COUNTRY, KEY_SHORTCUT};
		Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null);
		return cursor.getCount() == 0;
	}
	
	public Cursor getAllEntries()
	{
		String[] columns = {KEY_ID, KEY_COUNTRY, KEY_SHORTCUT};
		return db.query(DB_TABLE, columns, null, null, null, null, null);
	}
	
	public Country getEntry(long index)
	{
		String[] columns = {KEY_ID, KEY_COUNTRY, KEY_SHORTCUT};
		String where = "KEY_ID=" + index;
		
		Cursor cursor = db.query(true, DB_TABLE, columns, where, null, null, null, null, null);
		
		Country country = new Country(cursor.getString(COUNTRY_COLUMN), cursor.getString(SHORTCUT_COLUMN));
		return country;
	}
}
